﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Models.Data;
using Sider;
using Newtonsoft.Json;

namespace BlackBBS.Infrastructure.Repository
{
    public class SiderRepository<T> : RepositoryBase, IDisposable, IRepository<T> where T : IRepositoryEntity
    {
        private RedisClient<List<T>> _client = null;

        public string UrnKey { get; set; }

        public SiderRepository(string urnKey, string host)
        {
            this.UrnKey = urnKey;
            this._client = new RedisClient<List<T>>(host);            
        }
        public void Open()
        {
            try
            {
                // nothing for this
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Unable to Open [{0}]: [{1}]", this.UrnKey, ex.Message));
            }            
        }
        public void Close()
        {
            try
            {
                // nothing for this
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Unable to Close [{0}]: [{1}]", this.UrnKey, ex.Message));
            }  
            finally
            {
            
            }
        }
        public long Count()
        {
            var result = this.GetAll().Count();
            return result;
        }
        public long NextId()
        {
            var result = this.Count() + 1;
            return result;
        }
        public bool Delete(long key)
        {
            try
            {
                var data = this.GetAll().Where(x => x.Id != key).ToList();
                this._client.Set(this.UrnKey, data);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public T Get(long key)
        {
            var data = this.GetAll().Where(x => x.Id.Equals(key)).FirstOrDefault();
            return data == null ? default(T) : data;
        }

        public IEnumerable<T> GetAll()
        {
            return this._client.Get(this.UrnKey) ?? new List<T>();
        }
        public void Save(T entity)
        {
            var list = this.GetAll().ToList();
            int index = list.FindIndex(x => x.Id.Equals(entity.Id));
            if (index < 0 || entity.Id == 0)
            {
                if (entity.Id == 0)
                {
                    entity.Id = list.Count() + 1;
                }
                list.Add(entity);
            }
            else
            {
                list[index] = entity;
            }
            this._client.Set(this.UrnKey, list);
        }
        public void Dispose()
        {
            try
            {
                if (this._client != null)
                {
                    this._client.Dispose();
                }
            }
            finally
            {
                this._client = null;
            }
        }
    }
}
